
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for add_news.
	''' </summary>
	Public Partial Class add_news
		Inherits BaseWebPage
		Protected pageTypeID As Integer = -1
		Protected col As Integer = DirectCast(ArticleItem.ArticleItemType.ritNews, Integer)
		Protected user As User

		Protected id As Integer = -1

		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.ADD_NEWS_PAGE;

			user = Me.LoggedUser
			If user = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_NEWS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If user.Status <> Constants.appAdmin Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = Nothing OrElse Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			If Request.Params("col") = Nothing OrElse Request.Params("col") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				pageTypeID = Convert.ToInt32(Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM))
				id = Convert.ToInt32(Request.Params("id"))
				col = Convert.ToInt32(Request.Params("col"))

				If col <> DirectCast(ArticleItem.ArticleItemType.ritInterview, Integer) AndAlso col <> DirectCast(ArticleItem.ArticleItemType.ritNews, Integer) Then
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End If
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End Try

			'txtContent.Text = "";
			txtContent.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_NEWS_CONTENT_MAX_CHARS.ToString()))
			txtContent.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_NEWS_CONTENT_MAX_CHARS.ToString()))

			' init special page name
			Dim oCurPage As SpecialPage = SpecialPages.GetSpecialPageByID(pageTypeID)
			lblPageName.Text = oCurPage.Name

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				trGroupRow.Visible = False
				cvUserGroup.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_VALIDATOR_MSG")
			Else
				trGroupRow.Visible = True
				ddlGroups.Attributes.Add("onchange", "document.forms[0]['" + ddlUsers.UniqueID + "'].selectedIndex=0")
				ddlUsers.Attributes.Add("onchange", "document.forms[0]['" + ddlGroups.UniqueID + "'].selectedIndex=0")
				cvUserGroup.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_VALIDATOR_MSG_BOTH")
			End If

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Not Me.IsPostBack Then
				Dim i As Integer
				Dim newsInterview As New ArticleItem()

				' init users list

				ddlUsers.Items.Add("Choose user")
				ddlUsers.Items.FindByText("Choose user").Value = ""
				Dim tmpUsrList As ArrayList = BO.SpecialPage.GetSpecialUsers(pageTypeID)
				Dim tmpUsr As BO.SpecialUser = Nothing
				Dim tmpUsrCount As Integer = tmpUsrList.Count
				Dim tmpLI As ListItem
				i = 0
				While i < tmpUsrCount
					tmpUsr = DirectCast(tmpUsrList(i), BO.SpecialUser)
					tmpLI = New ListItem(tmpUsr.Name + " " + tmpUsr.LastName, tmpUsr.ID.ToString())
					ddlUsers.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' init groups list

				ddlGroups.Items.Add("Choose group")
				ddlGroups.Items.FindByText("Choose group").Value = ""

				Dim tmpGrpList As ArrayList = BO.SpecialPage.GetSpecialGroups(pageTypeID)
				Dim tmpGrp As BO.SpecialGroup = Nothing
				Dim tmpGrpCount As Integer = tmpGrpList.Count
				i = 0
				While i < tmpGrpCount
					tmpGrp = DirectCast(tmpGrpList(i), BO.SpecialGroup)
					tmpLI = New ListItem(tmpGrp.Name, tmpGrp.ID.ToString())
					ddlGroups.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				If id <> 0 Then
					newsInterview = ArticleItem.GetArticleItemByID(id)
					txtTitle.Text = Server.HtmlDecode(newsInterview.Title)
					txtSubtitle.Text = Server.HtmlDecode(newsInterview.Subtitle)
					txtContent.Text = Server.HtmlDecode(newsInterview.Content)

					If newsInterview.UserID > 0 Then
						If Not SpecialUser.IsMemeber(newsInterview.UserID, newsInterview.SpecialPageID) Then
							Dim tmpSelectedUsr As BO.User = Users.GetUserByID(newsInterview.UserID)
							tmpLI = New ListItem(tmpSelectedUsr.Name + " " + tmpSelectedUsr.LastName, newsInterview.UserID.ToString())
							ddlUsers.Items.Add(tmpLI)
						End If
						ddlUsers.ClearSelection()
						ddlUsers.Items.FindByValue(newsInterview.UserID.ToString()).Selected = True
					End If

					If newsInterview.GroupID > 0 Then
						ddlGroups.ClearSelection()
						ddlGroups.Items.FindByValue(newsInterview.GroupID.ToString()).Selected = True
					End If
					If newsInterview.Img <> Nothing Then
						btnDelete.Visible = True
						imgNews.Visible = True
						imgNews.ImageUrl = Pictures.GetPictureSrcFullURL(-1, newsInterview.ID, Constants.IMAGE_IS_NEWS, "", "../")
					End If
				End If
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvUserGroup.ServerValidate, AddressOf Me.cvUserGroup_ServerValidate

		End Sub
#End Region

		Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim sTitle As String = removeScriptTags(Request.Params(txtTitle.UniqueID).Trim())
			Dim sSubtitle As String = removeScriptTags(Request.Params(txtSubtitle.UniqueID).Trim())
			Dim sContent As String = removeScriptTags(Request.Params(txtContent.UniqueID).Trim())
			Dim nUserID As Integer = -1
			Dim nGroupID As Integer = -1
			If Request.Params(ddlUsers.UniqueID).Trim() <> "" Then
				nUserID = Convert.ToInt32(Request.Params(ddlUsers.UniqueID).Trim())
			End If
			If (Constants.SHOW_GROUPS = Constants.SHOW_ON) AndAlso (Request.Params(ddlGroups.UniqueID).Trim() <> "") Then
				nGroupID = Convert.ToInt32(Request.Params(ddlGroups.UniqueID).Trim())
			End If

			If Page.IsValid Then
				Dim tmpSpecialPage As New SpecialPage()
				Dim tmpNewsInterview As New ArticleItem()
				tmpNewsInterview.ID = id
				tmpNewsInterview.SpecialPageID = pageTypeID
				If nUserID <> -1 Then
					tmpNewsInterview.UserID = nUserID
				End If
				If nGroupID <> -1 Then
					tmpNewsInterview.GroupID = nGroupID
				End If
				tmpNewsInterview.Title = Server.HtmlEncode(sTitle.Substring(0, Math.Min(sTitle.Length, Constants.TEXT_FIELD_NEWS_MAX_CHARS)))
				tmpNewsInterview.Subtitle = Server.HtmlEncode(sSubtitle.Substring(0, Math.Min(sSubtitle.Length, Constants.TEXT_FIELD_NEWS_MAX_CHARS)))
				tmpNewsInterview.Content = Server.HtmlEncode(sContent.Substring(0, Math.Min(sContent.Length, Constants.TEXT_NEWS_CONTENT_MAX_CHARS)))
				tmpNewsInterview.Type = DirectCast(col, ArticleItem.ArticleItemType)
				If fldFileUpload.PostedFile.ContentLength <> 0 Then
					tmpNewsInterview.SetPicture(fldFileUpload.PostedFile.InputStream)
				End If
				tmpNewsInterview.Save()
				Response.Redirect(Constants.SPECIAL + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageTypeID)
			End If
		End Sub

		Private Sub cvUserGroup_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			If (ddlUsers.SelectedIndex > 0) OrElse ((Constants.SHOW_GROUPS = Constants.SHOW_ON) AndAlso (ddlGroups.SelectedIndex > 0)) Then
				args.IsValid = True
			Else
				args.IsValid = False
			End If
		End Sub

		Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim oCurNews As ArticleItem = ArticleItem.GetArticleItemByID(id)
			If oCurNews = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If
			oCurNews.DoDeleteImg()
			btnDelete.Visible = False
			imgNews.Visible = False

		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






